package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ICamsActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.mapper.CamsRecordMapper;
import com.ruoyi.system.domain.CamsRecord;
import com.ruoyi.system.service.ICamsRecordService;

/**
 * 记录Service业务层处理
 *
 * @author anskory
 * @date 2024-04-24
 */
@Service
public class CamsRecordServiceImpl extends ServiceImpl<CamsRecordMapper, CamsRecord> implements ICamsRecordService {
    @Autowired
    private CamsRecordMapper camsRecordMapper;

    @Autowired
    private ICamsActivityService camsActivityService;

    /**
     * 查询记录列表
     *
     * @param camsRecord 记录
     * @return 记录
     */
    @Override
    public List<CamsRecord> selectCamsRecordList(CamsRecord camsRecord) {
        LambdaQueryWrapper<CamsRecord> lambdaQueryWrapper = new LambdaQueryWrapper<CamsRecord>();
        if (camsRecord.getUsername() != null && camsRecord.getUsername() != "")
            lambdaQueryWrapper.eq(CamsRecord::getUsername, camsRecord.getUsername());
        if (camsRecord.getActivityId() != null && camsRecord.getActivityId() != "")
            lambdaQueryWrapper.eq(CamsRecord::getActivityId, camsRecord.getActivityId());
        if (camsRecord.getRecordType() != null)
            lambdaQueryWrapper.eq(CamsRecord::getRecordType, camsRecord.getRecordType());
        return this.list(lambdaQueryWrapper);
    }

    //新增或修改记录
    @Override
    public boolean addOrUpdateRecord(String activityId, Integer recordType) {
        String username = SecurityUtils.getUsername();
        CamsRecord camsRecord = new CamsRecord();
        camsRecord.setUsername(username);
        camsRecord.setActivityId(activityId);
        camsRecord.setRecordType(recordType);
        camsRecord.setUpdateTime(DateUtils.getNowDate());
        camsRecord.setUpdateBy(username);
        if (this.getOne(new LambdaQueryWrapper<CamsRecord>().eq(CamsRecord::getRecordType, recordType).eq(CamsRecord::getUsername, username).eq(CamsRecord::getActivityId, activityId)) == null) {
            camsRecord.setCreateBy(username);
            camsRecord.setCreateTime(DateUtils.getNowDate());
            this.save(camsRecord);
            return true;
        }else if(recordType==0){
            this.update(camsRecord, new LambdaQueryWrapper<CamsRecord>().eq(CamsRecord::getRecordType, recordType).eq(CamsRecord::getUsername, username).eq(CamsRecord::getActivityId, activityId));
            return true;
        }else {
            this.remove(new LambdaQueryWrapper<CamsRecord>().eq(CamsRecord::getRecordType, recordType).eq(CamsRecord::getUsername, username).eq(CamsRecord::getActivityId, activityId));
            return false;
        }
    }

    //删除记录
    @Override
    public boolean removeRecord(String activityId, Integer recordType) {
        return this.remove(new LambdaQueryWrapper<CamsRecord>().eq(CamsRecord::getUsername, SecurityUtils.getUsername()).eq(CamsRecord::getRecordType, recordType).eq(CamsRecord::getActivityId, activityId));
    }

    //查询历史记录
    @Override
    public List<CamsRecord> historyList(String keyword) {
        String username = SecurityUtils.getUsername();
        return this.list(new LambdaQueryWrapper<CamsRecord>().eq(CamsRecord::getUsername, username).like(CamsRecord::getActivityId, keyword).or().like(CamsRecord::getActivityId, keyword));
    }

    @Override
    public List<CamsRecord> filterRecordsByType(List<CamsRecord> records, int type) {
        return records.stream()
                .filter(record -> record.getRecordType() == type)
                .collect(Collectors.toList());
    }

    @Override
    public boolean isRecordOwnedByUser(List<CamsRecord> records) {
        String username = SecurityUtils.getUsername();
        return records.stream()
                .anyMatch(record -> Objects.equals(record.getUsername(), username));
    }

}
